/**
 * Sends a file from a given form (object)
 * @param object The form
 * @returns {Boolean} False to prevent submitting the form
 */
function sendFile(object) {
	
	// All the variables
	var form = object.form;
	var formData = new FormData(form);
	var input = $(form).find('#inputFile');
	var submit = $(form).find('#submitFile');
	var progress_bar = $(form).find('#progress');
	var submit_init_value = submit.val();
	var xhr = new XMLHttpRequest();
	
	// SET THE CSS RULES
    input.attr('disabled','disabled');
    submit.val("Upload in progress...");
	submit.attr('disabled','disabled');
	progress_bar.attr('value',0);
	progress_bar.css("visibility", "visible");
	
	// PREPARE UPLOAD
	xhr.open('POST', form.action, true);
	xhr.onload = function() {
		console.log("Upload done (file)");
		var json = JSON.parse(this.responseText);
		var result = json['result'];
		var error = json['error'];
		$('#main h3 .success').text(typeof result != 'undefined' ? result : "");
		$('#main h3 .fail').text(typeof error != 'undefined' ? error : "");
	};
	xhr.onerror= function() {
		console.log("Upload file failed");
		$('#main h3 .success').text("");
		$('#main h3 .fail').text("");
		alert("Upload failed. Server seems offline.");
	};
	xhr.upload.onprogress = function(e) {
		progress_bar.attr('value',e.loaded);
		progress_bar.attr('max',e.total);
    };
    xhr.onloadend = function() {
    	input.val("");
		input.removeAttr('disabled');
		submit.removeAttr('disabled');
		submit.val(submit_init_value);
		progress_bar.css("visibility", "hidden");
    };
	
	// UPLOAD
	xhr.send(formData);

	return false; // Prevent page from submitting.
}


/**
 * Sends a link from a given form (object)
 * @param object The form
 * @returns {Boolean} False to prevent submitting the form
 */
function sendLink(object) {
	
	// All the variables
	var form = object.form;
	var formData = $(form).serialize();
	var input = $(form).find('#inputLink');
	var submit = $(form).find('#submitLink');
	
	var submit_init_value = submit.val();
	
    // SET THE CSS RULES
	input.attr('disabled','disabled');
    submit.val("Upload in progress...");
	submit.attr('disabled','disabled');
	input.css("background-position", "right 5px center");
	
	// UPLOAD
	$.ajax({
		type: "POST",
		url: form.action,
		data: formData,
		dataType: "json",
		timeout: 60000, // 60s
		success: function(data, textStatus, jqXHR) {
			console.log("Upload done (link)");
			var result = data['result'];
			var error = data['error'];
			$('#main h3 .success').text(typeof result != 'undefined' ? result : "");
			$('#main h3 .fail').text(typeof error != 'undefined' ? error : "");
		},
		error: function(jqXHR, textStatus, errorThrown) {
			 console.log("Upload link failed: " + textStatus);
			 $('#main h3 .success').text("");
			 $('#main h3 .fail').text("");
			 alert("Upload failed. Server seems offline.");
		},
		complete: function(jqXHR,textStatus) {
			input.val("");
			input.removeAttr('disabled');
			submit.removeAttr('disabled');
			submit.val(submit_init_value);
			input.css("background-position", "-9999px");
		}
	});

	return false; // Prevent page from submitting.
}